

<template>
  <div>
    <van-form @submit="onSubmit">
      <select-area @selectedArea="selectedArea" :areaName="areaName"/>
      <van-field
        v-model="hospital.name"  label="名称" 
        :rules="[{ required: true, message: '请填写名称' }]"/>      
      <van-field
        v-model="hospital.addr" type="text" label="地址" 
        :rules="[{ required: true, message: '请填写地址' }]"/>
      <van-field
        v-model="hospital.conn_phone" type="textarea" label="联系电话" 
        :rules="[{ required: true, message: '请填写电话，多个逗号分开' }]"/>
      <van-field
        v-model="hospital.order_no" type="digit" label="排序" 
        :rules="[{ required: true, message: '请填写序号' }]"/>
      <van-button round block type="info" native-type="submit">提交</van-button>
    </van-form>
  </div>
</template>

<script>
  import { insert,update } from "@/api/common";
  import SelectArea from "../components/SelectArea";
  import { Toast } from 'vant';

  export default {
    props:['hospital'],
    components: {
      SelectArea,
    },
    data() {
      return {
        items: [],
        areaValue: 0,
        areaName: '',
      }
    },
    watch: {
      hospital: {
        handler(newValue, oldValue) {
          this.areaValue = newValue.area;
          this.areaName = newValue.areaName;
        }
      }
    },
    mounted() {
    },
    methods: {   
    onSubmit(values) {
      if(this.areaValue==0) {
        Toast('请选择区域');
        return;
      }
      this.hospital.area = this.areaValue;
      var params={
        'Wh_hospital': this.hospital,
        'tag': 'Wh_hospital'
      }
      let that = this;
      if(hospital.id>0) {
        update(params)
          .then((res) => {
            if(res.data.msg=='success') {
              this.$emit('onAdded','');
              Toast.success('保存医院成功');
            }
            else
              Toast.fail('保存医院失败');
          })
          .catch(function (error){
              Toast.fail('调用服务器，保存医院失败');
          });
      }
      else {
        insert(params)
          .then((res) => {
            if(res.data.msg=='success') {
              this.$emit('onAdded','');
              Toast.success('保存医院成功');
            }
            else
              Toast.fail('保存医院失败');
          })
          .catch(function (error){
              Toast.fail('调用服务器，保存医院失败');
          });
        }
      },
      selectedArea(value) {
        this.areaValue = value;
      },
    }
  }
</script>

<style scoped>

</style>
  